<template>
  <div class="bg">
    <div class="login">
      <h2>智慧停车后台管理</h2>
      <!-- 用户名登录 -->
      <el-form ref="ruleFormRef" :model="user" status-icon :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="user.userName" placeholder="请输入用户名" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="user.pwd" placeholder="4-8英文数字字母_注册,字母开头" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="验证码" prop="verify">
          <el-input class="code" v-model="user.verify" type="text" weight="100px" />
          <img src="../../assets/code.png" class="code-img" alt="">

        </el-form-item>
        <!--  <el-radio-group v-model="role" class="role">
          <el-radio label="admin" size="large">管理员登录</el-radio>
          <el-radio label="normal" size="large">普通用户登录</el-radio>
        </el-radio-group> -->
        <div class="forget-remember">
          <el-checkbox v-model="checked" label="pass" size="large">记住用户名和密码</el-checkbox>
          <el-link type="primary">忘记密码？</el-link>
    </div>
    <!-- 登录 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit(ruleFormRef)">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
// import { userLogin } from '@/api/user'
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus'
import { userLogin } from '@/api/user';
import useIndexStore from '@/stores';
const useStore = useIndexStore()

const router = useRouter()
const pass = ref(false)
const checked = ref(localStorage.getItem('pwd')?true:false)
const user = reactive({
  userName: checked.value?localStorage.getItem('userName'):'',
  pwd: checked.value?localStorage.getItem('pwd'):'',
  verify: ''
})
const rules = reactive({
  userName: [
    {
      required: true,
      message: '用户名必填',
      trigger: 'blur'
    },
    {
      pattern: /^\w{1,5}$/,
      message: '1-5英文数字字母_注册'
    }
  ],
  pwd: [
    {
      required: true,
      message: '密码必填',
      trigger: 'blur'
    },
    {
      pattern: /[a-zA-Z]\w{3,7}/,
      message: '4-8英文数字字母_注册,字母开头'
    }
  ],
  verify: [
    {
      required: true,
      message: '验证码必填',
      trigger: 'blur'
    },
    {
      pattern: /cht7v/i,
      message: '请输入正确验证码'
    }
  ]
})
// 
// 输入错误，重置登录框
const clearValue = ()=>{
  user.pwd=''
  user.userName=''
  user.verify=''
}
// 登录·跳转·
const onSubmit = async (formEl: any) => {
  if (!formEl) return
  await formEl.validate((valid: any) => {
    if (valid) {
      userLogin({
        ...user,
      }).then((res) => {
        if (res.data.code === 200) {
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("role", res.data.data.role);
          // 储存 pinia仓库中
          useStore.initUser(res.data.data);
          ElMessage({
            type: "success",
            message: "用户登录成功",
            duration: 1500,
            onClose: () => {
              router.replace("/home");
            },
          });
        }
      });
    } else {
      ElMessage({
        message: '账号或密码不正确',
        type: 'error',
        duration: 1000,
        onClose: () => {
          clearValue()
        }
      })
    }
  })
}
// const ruleFormRef = ref()
/* const pwdLogin = async (formEl: any) => {
  if (!formEl) return
  await formEl.validate((valid: any) => {
    if (valid) {
      ElMessage({
        type: 'success',
        message: '登录成功',
        duration: 2000,
        onClose: () => {
          router.push('/home')
        }
      })
    } else {
      ElMessage({
        type: 'error',
        message: '登录失败',
        onClose: () => {
          resetForm()
        }
      })
    }
    // 点击记住用户名和密码
    if(checked.value){
      localStorage.setItem('userName',user.userName)
      localStorage.setItem('pwd',user.pwd)
      localStorage.setItem('remember',checked.value)
    }else{
      localStorage.removeItem('userName')
      localStorage.removeItem('pwd')
      localStorage.removeItem('remember')
    }
  })
} */



</script>

<style scoped lang="scss">
.bg {
  background-image: url(../../assets/bg.jpg);
  width: 100%;
  height: 100%;
  position: absolute;
  background-size: cover;
  display: flex;
}

.login {
  // margin: auto ;
  margin: 100px 50px 0 850px;
  width: 500px;
  height: 360px;
  background-color: rgba($color: #f5f8fb, $alpha: 0.5);
  border-radius: 10px;
  padding: 40px;
  padding-right: 80px;
  box-sizing: border-box;
  color: rgb(33, 33, 33);
}

.login>h2 {
  text-align: center;
  margin-bottom: 30px;
}

.code {
  width: 120px;
}

.code-img {
  width: 100px;
  margin-left: 10px;
}

.forget-remember {
  width: 300px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-left: 70px;
  margin-bottom: 20px;
  color: #4FC0FF
}
</style>